import { defineStore } from 'pinia'
import { ref } from 'vue'
import { reportApi } from '@/api/report'
import type { DailyReport } from '@/types'

export const useReportStore = defineStore('report', () => {
  // 状态
  const reports = ref<DailyReport[]>([])
  const currentReport = ref<DailyReport | null>(null)
  const loading = ref(false)
  const todayReported = ref(false)
  const weeklyStats = ref<any>(null)
  const monthlyStats = ref<any>(null)

  // 获取所有日报
  const fetchReports = async () => {
    loading.value = true
    try {
      const response = await reportApi.getAll()
      reports.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取我的日报
  const fetchMyReports = async () => {
    loading.value = true
    try {
      const response = await reportApi.getMy()
      reports.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据项目ID获取日报
  const fetchReportsByProject = async (projectId: number) => {
    loading.value = true
    try {
      const response = await reportApi.getByProject(projectId)
      reports.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据用户ID获取日报
  const fetchReportsByUser = async (userId: number) => {
    loading.value = true
    try {
      const response = await reportApi.getByUser(userId)
      reports.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据日期范围获取日报
  const fetchReportsByDateRange = async (startDate: string, endDate: string) => {
    loading.value = true
    try {
      const response = await reportApi.getByDateRange(startDate, endDate)
      reports.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据用户ID和日期范围获取日报
  const fetchReportsByUserAndDateRange = async (userId: number, startDate: string, endDate: string) => {
    loading.value = true
    try {
      const response = await reportApi.getByUserAndDateRange(userId, startDate, endDate)
      reports.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取今日日报
  const fetchTodayReport = async () => {
    loading.value = true
    try {
      const response = await reportApi.getToday()
      currentReport.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取日报详情
  const fetchReport = async (id: number) => {
    loading.value = true
    try {
      const response = await reportApi.getById(id)
      currentReport.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 创建日报
  const createReport = async (reportData: Omit<DailyReport, 'id' | 'createdAt' | 'updatedAt'>) => {
    loading.value = true
    try {
      const response = await reportApi.create(reportData)
      reports.value.unshift(response)
      currentReport.value = response
      todayReported.value = true
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新日报
  const updateReport = async (id: number, reportData: Partial<DailyReport>) => {
    loading.value = true
    try {
      const response = await reportApi.update(id, reportData)
      const index = reports.value.findIndex(r => r.id === id)
      if (index !== -1) {
        reports.value[index] = response
      }
      if (currentReport.value?.id === id) {
        currentReport.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 删除日报
  const deleteReport = async (id: number) => {
    loading.value = true
    try {
      await reportApi.delete(id)
      reports.value = reports.value.filter(r => r.id !== id)
      if (currentReport.value?.id === id) {
        currentReport.value = null
      }
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 检查今日是否已提交日报
  const checkTodayReported = async () => {
    loading.value = true
    try {
      const response = await reportApi.checkTodayReported()
      todayReported.value = response.reported
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取每周工作时长统计
  const fetchWeeklyStats = async () => {
    loading.value = true
    try {
      const response = await reportApi.getMyWorkTimeStats()
      weeklyStats.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取每月工作时长统计
  const fetchMonthlyStats = async () => {
    loading.value = true
    try {
      const response = await reportApi.getMyWorkTimeStats()
      monthlyStats.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 清空当前日报
  const clearCurrentReport = () => {
    currentReport.value = null
  }

  return {
    // 状态
    reports,
    currentReport,
    loading,
    todayReported,
    weeklyStats,
    monthlyStats,
    // 方法
    fetchReports,
    fetchMyReports,
    fetchReportsByProject,
    fetchReportsByUser,
    fetchReportsByDateRange,
    fetchReportsByUserAndDateRange,
    fetchTodayReport,
    fetchReport,
    createReport,
    updateReport,
    deleteReport,
    checkTodayReported,
    fetchWeeklyStats,
    fetchMonthlyStats,
    clearCurrentReport
  }
})